<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Spring参考文档 | 恒星同学</title><meta name="keywords" content="Spring"><meta name="author" content="hengxingstu"><meta name="copyright" content="hengxingstu"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Author：www.javaboy.org Version：0.0.1  [TOC] 一、SpringMVC 1.1 引言 java开源框架，Spring Framework的一个独立模块。 MVC框架,在项目中开辟MVC层次架构     对控制器中的功能 包装 简化 扩展践行工厂模式，功能架构在工厂之上  1.2 MVC架构1.2.1 概念   名称 职责    Model 模型：即业务模">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring参考文档">
<meta property="og:url" content="https://hengxingstu.gitee.io/2023/02/21/code/springMVC%E6%96%87%E6%A1%A3/index.html">
<meta property="og:site_name" content="恒星同学">
<meta property="og:description" content="Author：www.javaboy.org Version：0.0.1  [TOC] 一、SpringMVC 1.1 引言 java开源框架，Spring Framework的一个独立模块。 MVC框架,在项目中开辟MVC层次架构     对控制器中的功能 包装 简化 扩展践行工厂模式，功能架构在工厂之上  1.2 MVC架构1.2.1 概念   名称 职责    Model 模型：即业务模">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2023/02/21/vdVhPU5RITfrXg4.jpg">
<meta property="article:published_time" content="2023-02-21T12:26:00.000Z">
<meta property="article:modified_time" content="2023-02-21T12:32:19.506Z">
<meta property="article:author" content="hengxingstu">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2023/02/21/vdVhPU5RITfrXg4.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://hengxingstu.gitee.io/2023/02/21/code/springMVC%E6%96%87%E6%A1%A3/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"prismjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Spring参考文档',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-02-21 20:32:19'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://wallpapercave.com/uwp/uwp2365506.jpeg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">36</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/gallery/"><i class="fa-fw fas fa-images"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw fa-fw fas fa-comment-dots"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2023/02/16/N8E1LGTUnYVdmPk.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">恒星同学</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/gallery/"><i class="fa-fw fas fa-images"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw fa-fw fas fa-comment-dots"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Spring参考文档</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-02-21T12:26:00.000Z" title="发表于 2023-02-21 20:26:00">2023-02-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-02-21T12:32:19.506Z" title="更新于 2023-02-21 20:32:19">2023-02-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Spring/">Spring</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Spring参考文档"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/2023/02/21/code/springMVC%E6%96%87%E6%A1%A3/#post-comment"><span id="twikoo-count"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><img src="/Pictures/logo.jpg" alt="logo"></p>
<blockquote>
<p>Author：<a target="_blank" rel="noopener" href="http://www.javaboy.org/">www.javaboy.org</a></p>
<p>Version：0.0.1</p>
</blockquote>
<p>[TOC]</p>
<h3 id="一、SpringMVC"><a href="#一、SpringMVC" class="headerlink" title="一、SpringMVC"></a>一、SpringMVC</h3><hr>
<h4 id="1-1-引言"><a href="#1-1-引言" class="headerlink" title="1.1 引言"></a>1.1 引言</h4><blockquote>
<p><strong>java开源框架，Spring Framework的一个独立模块。</strong></p>
<p><strong>MVC框架,在项目中开辟MVC层次架构</strong>    </p>
<p><strong>对控制器中的功能 包装 简化 扩展践行工厂模式，功能架构在工厂之上</strong></p>
</blockquote>
<h4 id="1-2-MVC架构"><a href="#1-2-MVC架构" class="headerlink" title="1.2 MVC架构"></a>1.2 MVC架构</h4><h5 id="1-2-1-概念"><a href="#1-2-1-概念" class="headerlink" title="1.2.1 概念"></a>1.2.1 概念</h5><table>
<thead>
<tr>
<th>名称</th>
<th>职责</th>
</tr>
</thead>
<tbody><tr>
<td>Model</td>
<td>模型：即业务模型，负责完成业务中的数据通信处理，对应项目中的 service和dao</td>
</tr>
<tr>
<td>View</td>
<td>视图：渲染数据，生成页面。对应项目中的Jsp</td>
</tr>
<tr>
<td>Controller</td>
<td>控制器：直接对接请求，控制MVC流程，调度模型，选择视图。对应项目中的Servlet</td>
</tr>
</tbody></table>
<h5 id="1-2-2-好处"><a href="#1-2-2-好处" class="headerlink" title="1.2.2 好处"></a>1.2.2 好处</h5><blockquote>
<ul>
<li><p>MVC是现下软件开发中的最流行的代码结构形态;</p>
</li>
<li><p>人们根据负责的不同逻辑，将项目中的代码分成 M V C 3个层次;</p>
</li>
<li><p>层次内部职责单一，层次之间耦合度低;</p>
</li>
<li><p>符合低耦合 高内聚的设计理念。也实际有利于项目的长期维护。</p>
</li>
</ul>
</blockquote>
<h3 id="二、开发流程"><a href="#二、开发流程" class="headerlink" title="二、开发流程"></a>二、开发流程</h3><hr>
<h4 id="2-1-导入依赖"><a href="#2-1-导入依赖" class="headerlink" title="2.1 导入依赖"></a>2.1 导入依赖</h4><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-webmvc<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>5.1.6.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="2-2-配置核心-前端-控制器"><a href="#2-2-配置核心-前端-控制器" class="headerlink" title="2.2 配置核心(前端)控制器"></a>2.2 配置核心(前端)控制器</h4><blockquote>
<p>作为一个MVC框架，首先要解决的是：如何能够收到请求！</p>
<p>所以MVC框架大都会设计一款前端控制器，选型在 Servlet 或 Filter两者之一,在框架最前沿率先工作，接收所有请求。</p>
<p>此控制器在接收到请求后，还会负责springMVC的核心的调度管理，所以既是前端又是核心。</p>
</blockquote>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">></span></span>mvc<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-class</span><span class="token punctuation">></span></span>org.springframework.web.servlet.DispatcherServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-class</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- 局部参数：声明配置文件位置 --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>init-param</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-name</span><span class="token punctuation">></span></span>contextConfigLocation<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-name</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-value</span><span class="token punctuation">></span></span>classpath:mvc.xml<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-value</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>init-param</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- Servlet启动时刻：可选 --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>load-on-startup</span><span class="token punctuation">></span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>load-on-startup</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-mapping</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">></span></span>mvc<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">></span></span>/<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-mapping</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="2-3-后端控制器"><a href="#2-3-后端控制器" class="headerlink" title="2.3 后端控制器"></a>2.3 后端控制器</h4><blockquote>
<p>等价于之前定义的Servlet</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Controller</span> <span class="token comment">//声明这是一个控制器</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/hello"</span><span class="token punctuation">)</span>  <span class="token comment">//访问路径 ，等价于url-pattern</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloController</span> <span class="token punctuation">&#123;</span>
	<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>  <span class="token comment">//访问路径</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span> <span class="token comment">// 跳转:/index.jsp  </span>
	<span class="token punctuation">&#125;</span>
	<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test2"</span><span class="token punctuation">)</span> <span class="token comment">//访问路径</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello c9"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token string">"views/users"</span><span class="token punctuation">;</span><span class="token comment">//  跳转:/views/user.jsp</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="2-4-配置文件"><a href="#2-4-配置文件" class="headerlink" title="2.4 配置文件"></a>2.4 配置文件</h4><blockquote>
<p>默认名称：核心控制器名-servet.xml     默认位置：WEB-INF</p>
<p>随意名称：mvc.xml           随意位置：resources    但需要配置在核心控制器中</p>
</blockquote>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>beans</span> 	<span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans<span class="token punctuation">"</span></span>
		<span class="token attr-name"><span class="token namespace">xmlns:</span>context</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/context<span class="token punctuation">"</span></span>
		<span class="token attr-name"><span class="token namespace">xmlns:</span>mvc</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/mvc<span class="token punctuation">"</span></span> 
		<span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> 
		<span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans
							http://www.springframework.org/schema/beans/spring-beans.xsd
							http://www.springframework.org/schema/context
							http://www.springframework.org/schema/context/spring-context.xsd
							http://www.springframework.org/schema/mvc
							http://www.springframework.org/schema/mvc/spring-mvc.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>

	<span class="token comment">&lt;!-- 告知springmvc  哪些包中 存在 被注解的类 --></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">context:</span>component-scan</span> <span class="token attr-name">base-package</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.qf.controller<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">context:</span>component-scan</span><span class="token punctuation">></span></span>
	<span class="token comment">&lt;!-- 注册注解开发驱动 --></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>annotation-driven</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">mvc:</span>annotation-driven</span><span class="token punctuation">></span></span>
	<span class="token comment">&lt;!-- 视图解析器
	     作用：1.捕获后端控制器的返回值="index"
	          2.解析： 在返回值的前后 拼接 ==> "/index.jsp"
	 --></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.springframework.web.servlet.view.InternalResourceViewResolver<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
		<span class="token comment">&lt;!-- 前缀 --></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>prefix<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>property</span><span class="token punctuation">></span></span>
		<span class="token comment">&lt;!-- 后缀 --></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>suffix<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>.jsp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>property</span><span class="token punctuation">></span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>bean</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>beans</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="2-5-访问"><a href="#2-5-访问" class="headerlink" title="2.5 访问"></a>2.5 访问</h4><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown">http://localhost：8989/hello/test1
http://localhost：8989/hello/test2<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<p>接口方法的返回值：</p>
<ol>
<li>没有返回值</li>
<li>返回逻辑视图名</li>
<li>返回字符串</li>
<li>返回 JSON</li>
</ol>
<h3 id="三、接收请求参数"><a href="#三、接收请求参数" class="headerlink" title="三、接收请求参数"></a>三、接收请求参数</h3><hr>
<h4 id="3-1-基本类型参数"><a href="#3-1-基本类型参数" class="headerlink" title="3.1 基本类型参数"></a>3.1 基本类型参数</h4><blockquote>
<p>请求参数和方法的形参 同名即可</p>
</blockquote>
<blockquote>
<p>springMVC默认可以识别的日期字符串格式为： yyyy&#x2F;MM&#x2F;dd HH:mm:ss<br>通过@DateTimeFormat可以修改默认日志格式</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">// id  name gender</span>
<span class="token comment">// http://localhost:8989/xxx/../test1?id=1&amp;name=zzz&amp;gender=false&amp;birth=2018-12-12 12:20:30</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testParam1</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">,</span>
                         <span class="token class-name">String</span> name<span class="token punctuation">,</span>
                         <span class="token class-name">Boolean</span> gender<span class="token punctuation">,</span>
                         <span class="token annotation punctuation">@DateTimeFormat</span><span class="token punctuation">(</span>pattern<span class="token operator">=</span><span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">)</span><span class="token class-name">Date</span> birth<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"test param1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="3-2-实体收参【重点】"><a href="#3-2-实体收参【重点】" class="headerlink" title="3.2 实体收参【重点】"></a>3.2 实体收参【<code>重点</code>】</h4><blockquote>
<p>请求参数和实体的属性 同名即可</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token annotation punctuation">@DateTimeFormat</span><span class="token punctuation">(</span>pattern<span class="token operator">=</span><span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">)</span>
	<span class="token keyword">private</span> <span class="token class-name">Date</span> birth<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token class-name">Boolean</span> gender<span class="token punctuation">;</span>
	<span class="token comment">//set/get ...</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//http://localhost:8989/.../test2?id=1&amp;name=zzz&amp;gender=false&amp;birth=2018-12-12 12:20:30</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test2"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testParam2</span><span class="token punctuation">(</span><span class="token class-name">User</span> user<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"test param2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"user:"</span><span class="token operator">+</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="3-3-数组收参"><a href="#3-3-数组收参" class="headerlink" title="3.3 数组收参"></a>3.3 数组收参</h4><blockquote>
<p>简单类型的 数组</p>
</blockquote>
<pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">></span></span>
    ......
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>checkbox<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>hobby<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>fb<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>足球 
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>checkbox<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>hobby<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bb<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>篮球 
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>checkbox<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>hobby<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>vb<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>排球
    
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//http://localhost:8989/.../test3?hobby=football&amp;hobby=basketball</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test3"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testParam3</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> hobby<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">String</span> h<span class="token operator">:</span>hobby<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>h<span class="token operator">+</span><span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="3-4-集合收参-【了解】"><a href="#3-4-集合收参-【了解】" class="headerlink" title="3.4 集合收参 【了解】"></a>3.4 集合收参 【了解】</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserList</span> <span class="token punctuation">&#123;</span>
	<span class="token comment">//private User[] users;</span>
	<span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">></span></span> users<span class="token punctuation">;</span>
	<span class="token comment">//set/get..</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// &lt;input type="text" name="users[0].id"/></span>
<span class="token comment">// post请求：http://...?users[0].id=1&amp;users[0].name=zhangsan&amp;users[0].birth=2018-12-12&amp;users[1].id=2&amp;....</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test4"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testParam4</span><span class="token punctuation">(</span><span class="token class-name">UserList</span> userList<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">User</span> user<span class="token operator">:</span>userList<span class="token punctuation">.</span><span class="token function">getUsers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="3-5-路径参数"><a href="#3-5-路径参数" class="headerlink" title="3.5 路径参数"></a>3.5 路径参数</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">// &#123;id&#125; 定义名为id的路径；【/hello/&#123;id&#125;】的匹配能力和【/hello/*】等价</span>
<span class="token comment">// http://localhost:8989/.../hello/10   &#123;id&#125;匹配到10</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/hello/&#123;id&#125;"</span><span class="token punctuation">)</span>
<span class="token comment">// @PathVariable将&#123;id&#125;路径匹配到值赋给id参数</span>
<span class="token comment">// 路径名和参数名相同则@PathVariable("id")可简写为 @PathVariable</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testParam5</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"id:"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>            
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// http://localhost:8989/.../hello/tom   &#123;username&#125;匹配到tom</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/hello/&#123;username&#125;"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testParam6</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">"username"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//将&#123;username&#125;路径匹配到的值赋给name参数</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"username:"</span><span class="token operator">+</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="3-6-中文乱码"><a href="#3-6-中文乱码" class="headerlink" title="3.6 中文乱码"></a>3.6 中文乱码</h4><blockquote>
<p>首先，页面中字符集统一</p>
</blockquote>
<pre class="line-numbers language-jsp" data-language="jsp"><code class="language-jsp">JSP : &lt;%@page  pageEncoding&#x3D;&quot;utf-8&quot; %&gt;
HTML : &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<blockquote>
<p>其次，tomcat中字符集设置，对get请求中，中文参数乱码有效</p>
</blockquote>
<pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown">Tomcat配置：URIEncoding=utf-8<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<blockquote>
<p>最后，设置此filter，对post请求中，中文参数乱码有效</p>
</blockquote>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- 此过滤器会进行：request.setCharactorEncoding("utf-8"); --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>filter</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>filter-name</span><span class="token punctuation">></span></span>encoding<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>filter-name</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>filter-class</span><span class="token punctuation">></span></span>org.springframework.web.filter.CharacterEncodingFilter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>filter-class</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>init-param</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-name</span><span class="token punctuation">></span></span>encoding<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-name</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-value</span><span class="token punctuation">></span></span>utf-8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-value</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>init-param</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>filter</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>filter-mapping</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>filter-name</span><span class="token punctuation">></span></span>encoding<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>filter-name</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">></span></span>/*<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>filter-mapping</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="四、跳转"><a href="#四、跳转" class="headerlink" title="四、跳转"></a>四、跳转</h3><hr>
<h4 id="4-1-转发"><a href="#4-1-转发" class="headerlink" title="4.1 转发"></a>4.1 转发</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/forw"</span><span class="token punctuation">)</span>
<span class="token keyword">class</span> <span class="token class-name">ForwardController</span><span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testForward</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"test forward1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 转发跳转 /views/users.jsp</span>
        <span class="token comment">// return "views/users";//和下一行等价</span>
        <span class="token keyword">return</span> <span class="token string">"forward:/views/users.jsp"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test2"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testForward2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"test forward2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//转发到  /forw/test1</span>
        <span class="token comment">//return "forward:test1";//相对路径(转发到本类中的test1)</span>
        <span class="token comment">//转发到  /forw/test1</span>
        <span class="token keyword">return</span> <span class="token string">"forward:/forw/test1"</span><span class="token punctuation">;</span> <span class="token comment">//绝对路径</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="4-2-重定向"><a href="#4-2-重定向" class="headerlink" title="4.2 重定向"></a>4.2 重定向</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/redir"</span><span class="token punctuation">)</span>
<span class="token keyword">class</span> <span class="token class-name">RedirectController</span><span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testRedirect1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"test redirect1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//重定向到 /redir/test1</span>
        <span class="token comment">//return "redirect:test1"; //相对路径(转发到本类中的test1)</span>
        <span class="token keyword">return</span> <span class="token string">"redirect:/redir/test1"</span><span class="token punctuation">;</span><span class="token comment">//绝对路径</span>
    <span class="token punctuation">&#125;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test2"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testRedirect2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"test redirect2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//重定向到 /views/users.jsp</span>
        <span class="token keyword">return</span> <span class="token string">"redirect:/view/user.jsp"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="4-3-跳转细节"><a href="#4-3-跳转细节" class="headerlink" title="4.3 跳转细节"></a>4.3 跳转细节</h4><blockquote>
<ul>
<li><p>在增删改之后，为了防止请求重复提交，重定向跳转</p>
</li>
<li><p>在查询之后，可以做转发跳转</p>
</li>
</ul>
</blockquote>
<h3 id="五、传值"><a href="#五、传值" class="headerlink" title="五、传值"></a>五、传值</h3><p>从处理器往页面传值的四种方式：</p>
<ol>
<li>传统的 HttpServletRequest、HttpSession。</li>
<li>返回逻辑视图名，则可以通过 Model 参数传参。</li>
<li>返回 ModelAndView。</li>
<li>SessionAttribute（可以用在重定向的场景下）</li>
</ol>
<hr>
<blockquote>
<p>C得到数据后，跳转到V，并向V传递数据。进而V中可以渲染数据，让用户看到含有数据的页面</p>
<p>转发跳转：Request作用域</p>
<p>重定向跳转：Session作用域</p>
</blockquote>
<h4 id="5-1-Request和Session"><a href="#5-1-Request和Session" class="headerlink" title="5.1 Request和Session"></a>5.1 Request和Session</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//形参中 即可获得 request 和 session对象</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testData</span><span class="token punctuation">(</span><span class="token class-name">HttpSession</span> session<span class="token punctuation">,</span><span class="token class-name">HttpServletRequest</span> req，<span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    session<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    req<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    req<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">"users"</span><span class="token punctuation">,</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//return "test2";</span>
    <span class="token keyword">return</span> <span class="token string">"forward:/WEB-INF/test2.jsp"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="5-2-JSP中取值"><a href="#5-2-JSP中取值" class="headerlink" title="5.2 JSP中取值"></a>5.2 JSP中取值</h4><blockquote>
<p>建议：重点复习 EL  JSTL</p>
</blockquote>
<pre class="line-numbers language-jsp" data-language="jsp"><code class="language-jsp">&#x2F;&#x2F;jsp中用EL表达式 取值即可
&lt;fmt:formatDate value&#x3D;&quot;$&#123;sessionScope.user.birth&#125;&quot; pattern&#x3D;&quot;yyyy-MM-dd&quot;&#x2F;&gt; &lt;br&#x2F;&gt;
$&#123;sessionScope.user.birth&#125; &lt;br&gt;
$&#123;requestScope.age&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="5-3-Model"><a href="#5-3-Model" class="headerlink" title="5.3 Model"></a>5.3 Model</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//model中的数据，会在V渲染之前，将数据复制一份给request</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">testData</span><span class="token punctuation">(</span><span class="token class-name">Model</span> model<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    model<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"张三"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//jsp中用EL表达式 取值即可</span>
$<span class="token punctuation">&#123;</span>requestScope<span class="token punctuation">.</span>name<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="5-4-ModelAndView"><a href="#5-4-ModelAndView" class="headerlink" title="5.4 ModelAndView"></a>5.4 ModelAndView</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//modelandview 可以集中管理 跳转和数据</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">ModelAndView</span> <span class="token function">testData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//返回值类型为ModelAndView</span>
    <span class="token comment">//新建ModelAndView对象</span>
    <span class="token class-name">ModelAndView</span> mv <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ModelAndView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 设置视图名，即如何跳转</span>
    mv<span class="token punctuation">.</span><span class="token function">setViewName</span><span class="token punctuation">(</span><span class="token string">"forward:/index.jsp"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 增加数据</span>
    mv<span class="token punctuation">.</span><span class="token function">addObject</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">,</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> mv<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//jsp中用EL表达式 取值即可</span>
$<span class="token punctuation">&#123;</span>requestScope<span class="token punctuation">.</span>age<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="5-5-SessionAttributes"><a href="#5-5-SessionAttributes" class="headerlink" title="5.5 @SessionAttributes"></a>5.5 @SessionAttributes</h4><blockquote>
<ul>
<li><p>@SessionAttributes({“gender”,”name”})  ：model中的 name和gender 会存入session中</p>
</li>
<li><p>SessionStatus 移除session</p>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Controller</span>
<span class="token annotation punctuation">@SessionAttributes</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span><span class="token string">"gender"</span><span class="token punctuation">,</span><span class="token string">"name"</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span> <span class="token comment">// model中的 name和gender 会存入session中</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token punctuation">&#123;</span>

    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/hello"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token class-name">Model</span> m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        m<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"gender"</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 会存入session</span>
        mv<span class="token punctuation">.</span><span class="token function">addObject</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span><span class="token string">"zhj"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 会存入session</span>
        <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/hello2"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token class-name">SessionStatus</span> status<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">// 移除通过SessionAttributes存入的session</span>
        status<span class="token punctuation">.</span><span class="token function">setComplete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="六、静态资源"><a href="#六、静态资源" class="headerlink" title="六、静态资源"></a>六、静态资源</h3><hr>
<h4 id="6-1-静态资源问题"><a href="#6-1-静态资源问题" class="headerlink" title="6.1 静态资源问题"></a>6.1 静态资源问题</h4><blockquote>
<p>静态资源：html，js文件，css文件，图片文件</p>
</blockquote>
<blockquote>
<p>静态文件没有url-pattern,所以默认是访问不到的，之所以可以访问，是因为，tomcat中有一个全局的servlet：org.apache.catalina.servlets.DefaultServlet，它的url-pattern是 “&#x2F;“,是全局默认的Servlet.  所以每个项目中不能匹配的静态资源的请求，有这个Servlet来处理即可。</p>
</blockquote>
<blockquote>
<p>但，在SpringMVC中DispatcherServlet也采用了 “&#x2F;” 作为url-pattern, 则项目中不会再使用全局的Serlvet，则静态资源不能完成访问。</p>
</blockquote>
<h4 id="6-2-解决方案1"><a href="#6-2-解决方案1" class="headerlink" title="6.2 解决方案1"></a>6.2 解决方案1</h4><blockquote>
<p>DispathcerServlet采用其他的url-pattern</p>
<p>此时，所有访问handler的路径都要以 action结尾！！</p>
</blockquote>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet</span><span class="token punctuation">></span></span>
  	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">></span></span>mvc9<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">></span></span>
  	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-class</span><span class="token punctuation">></span></span>org.springframework.web.servlet.DispatcherServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-class</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-mapping</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">></span></span>mvc9<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">></span></span>*.action<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-mapping</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="6-3-解决方案2"><a href="#6-3-解决方案2" class="headerlink" title="6.3 解决方案2"></a>6.3 解决方案2</h4><blockquote>
<p>DispathcerServlet的url-pattern依然采用 “&#x2F;“,但追加配置</p>
</blockquote>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- 
  额外的增加一个handler，且其requestMapping:  "/**" 可以匹配所有请求，但是优先级最低
  所以如果其他所有的handler都匹配不上，请求会转向 "/**" ,恰好，这个handler就是处理静态资源的
  处理方式：将请求转会到tomcat中名为default的Servlet
  --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>default-servlet-handler</span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="6-4-解决方案3"><a href="#6-4-解决方案3" class="headerlink" title="6.4 解决方案3"></a>6.4 解决方案3</h4><blockquote>
<ul>
<li>mapping是访问路径，location是静态资源存放的路径</li>
<li>将&#x2F;html&#x2F;** 中 &#x2F;**匹配到的内容，拼接到 &#x2F;hhh&#x2F;后<br>http:&#x2F;&#x2F;….&#x2F;html&#x2F;a.html  访问 &#x2F;hhh&#x2F;a.html</li>
</ul>
</blockquote>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>resources</span> <span class="token attr-name">mapping</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/html/**<span class="token punctuation">"</span></span> <span class="token attr-name">location</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/hhh/<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="七、Json处理"><a href="#七、Json处理" class="headerlink" title="七、Json处理"></a>七、Json处理</h3><hr>
<p>主流处理方案三种：</p>
<ul>
<li>jackson（推荐）</li>
<li>gson</li>
<li>fastjson（了解）</li>
</ul>
<p>SpringMVC 中，默认提供了对 jackson 和 gson 的支持。</p>
<h4 id="7-1-导入依赖"><a href="#7-1-导入依赖" class="headerlink" title="7.1 导入依赖"></a>7.1 导入依赖</h4><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- Jackson springMVC默认的Json解决方案选择是 Jackson，所以只需要导入jackson的jar，即可使用。--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.fasterxml.jackson.core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>jackson-databind<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.9.8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="7-2-使用-ResponseBody"><a href="#7-2-使用-ResponseBody" class="headerlink" title="7.2 使用@ResponseBody"></a>7.2 使用@ResponseBody</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Controller</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JsonController</span><span class="token punctuation">&#123;</span>    
	<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@ResponseBody</span> <span class="token comment">//将handler的返回值，转换成json(jackson),并将json响应给客户端。</span>
    <span class="token keyword">public</span> <span class="token class-name">User</span> <span class="token function">hello1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> user<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">// @ResponseBody还可以用在handler的返回值上</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test2"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token annotation punctuation">@ResponseBody</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">></span></span> <span class="token function">hello2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">></span></span> users <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> users<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">// 如果返回值已经是字符串，则不需要转json，直接将字符串响应给客户端 </span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span>value<span class="token operator">=</span><span class="token string">"/test3"</span><span class="token punctuation">,</span>produces <span class="token operator">=</span> <span class="token string">"text/html;charset=utf-8"</span><span class="token punctuation">)</span> <span class="token comment">//produces 防止中文乱码</span>
    <span class="token annotation punctuation">@ResponseBody</span> 
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"你好"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="7-3-使用-RestController"><a href="#7-3-使用-RestController" class="headerlink" title="7.3 使用@RestController"></a>7.3 使用@RestController</h4><blockquote>
<p>Controller类上加了@RestController注解，等价于在类中的每个方法上都加了@ResponseBody</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Controller</span>
<span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JsonController</span><span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">User</span> <span class="token function">hello1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> user<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">//@ResponseBody还可以用在handler的返回值上</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test2"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">></span></span> <span class="token function">hello2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">></span></span> users <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> users<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="7-4-使用-RequestBody"><a href="#7-4-使用-RequestBody" class="headerlink" title="7.4 使用@RequestBody"></a>7.4 使用@RequestBody</h4><blockquote>
<h4 id="RequestBody-接收Json参数"><a href="#RequestBody-接收Json参数" class="headerlink" title="@RequestBody, 接收Json参数"></a><strong>@RequestBody</strong>, 接收Json参数</h4></blockquote>
<h5 id="7-4-1-定义Handler"><a href="#7-4-1-定义Handler" class="headerlink" title="7.4.1 定义Handler"></a>7.4.1 定义Handler</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">User</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Boolean</span> gender<span class="token punctuation">;</span>
    <span class="token comment">//set get</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/users"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">addUser</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">User</span> user<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//@RequestBody将请求体中的json数据转换为java对象</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"cap2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Post user :"</span><span class="token operator">+</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="7-4-2-Ajax发送json"><a href="#7-4-2-Ajax发送json" class="headerlink" title="7.4.2 Ajax发送json"></a>7.4.2 Ajax发送json</h5><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token keyword">var</span> xhr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XMLHttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
xhr<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"post"</span><span class="token punctuation">,</span><span class="token string">"$&#123;pageContext.request.contextPath&#125;/users?"</span><span class="token operator">+</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
xhr<span class="token punctuation">.</span><span class="token function">setRequestHeader</span><span class="token punctuation">(</span><span class="token string">"content-type"</span><span class="token punctuation">,</span><span class="token string">"application/json"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置请求头</span>
xhr<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'&#123;"id":1,"name":"shine","gender":"true"&#125;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//传递json串</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//ajax</span>
<span class="token keyword">var</span> user <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token literal-property property">id</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">"shine"</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
$<span class="token punctuation">.</span><span class="token function">ajax</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    <span class="token literal-property property">url</span><span class="token operator">:</span><span class="token string">'$&#123;pageContext.request.contextPath&#125;/json2/test4'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">type</span><span class="token operator">:</span><span class="token string">'post'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">contentType</span><span class="token operator">:</span><span class="token string">"application/json"</span><span class="token punctuation">,</span><span class="token comment">//声明请求参数类型为 json</span>
    <span class="token literal-property property">data</span><span class="token operator">:</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token comment">// 转换js对象成json</span>
    <span class="token function-variable function">success</span><span class="token operator">:</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">ret</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="7-5-Jackson常用注解"><a href="#7-5-Jackson常用注解" class="headerlink" title="7.5 Jackson常用注解"></a>7.5 Jackson常用注解</h4><h5 id="7-5-1-日期格式化"><a href="#7-5-1-日期格式化" class="headerlink" title="7.5.1 日期格式化"></a>7.5.1 日期格式化</h5><blockquote>
<p>@JsonFormat(pattern&#x3D;”yyyy-MM-dd HH:mm:ss”,timezone &#x3D; “GMT+8”)</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token annotation punctuation">@JsonFormat</span><span class="token punctuation">(</span>pattern<span class="token operator">=</span><span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">,</span>timezone <span class="token operator">=</span> <span class="token string">"GMT+8"</span><span class="token punctuation">)</span>
	<span class="token keyword">private</span> <span class="token class-name">Date</span> birth<span class="token punctuation">;</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    get<span class="token operator">/</span>set
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="7-5-2-属性名修改"><a href="#7-5-2-属性名修改" class="headerlink" title="7.5.2 属性名修改"></a>7.5.2 属性名修改</h5><blockquote>
<p>@JsonProperty(“new_name”)</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span><span class="token punctuation">&#123;</span>
	<span class="token annotation punctuation">@JsonProperty</span><span class="token punctuation">(</span><span class="token string">"new_id"</span><span class="token punctuation">)</span> <span class="token comment">//不再使用原属性名，而是 "new_id"</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    get<span class="token operator">/</span>set
<span class="token punctuation">&#125;</span>
输出的json：<span class="token punctuation">&#123;</span>“new_id”<span class="token operator">:</span>xx<span class="token punctuation">,</span><span class="token string">"name"</span><span class="token operator">:</span><span class="token string">"xx"</span><span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="7-5-3-属性忽略"><a href="#7-5-3-属性忽略" class="headerlink" title="7.5.3 属性忽略"></a>7.5.3 属性忽略</h5><blockquote>
<p>@JsonIgnore</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JsonIgnore</span> <span class="token comment">// 生成json时，忽略此属性</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    get<span class="token operator">/</span>set
<span class="token punctuation">&#125;</span>
输出json时<span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token string">"id"</span><span class="token operator">:</span>xx<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="7-5-4-null和empty属性排除"><a href="#7-5-4-null和empty属性排除" class="headerlink" title="7.5.4 null和empty属性排除"></a>7.5.4 null和empty属性排除</h5><blockquote>
<p>Jackson 默认会输出null值的属性，如果不需要，可以排除。</p>
<p>@JsonInclude(JsonInclude.Include.NON_NULL) &#x2F;&#x2F;null值 属性不输出<br>@JsonInclude(value&#x3D; JsonInclude.Include.NON_EMPTY) &#x2F;&#x2F; empty属性不输出( 空串，长度为0的集合，null值)</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JsonInclude</span><span class="token punctuation">(</span><span class="token class-name">JsonInclude<span class="token punctuation">.</span>Include</span><span class="token punctuation">.</span>NON_NULL<span class="token punctuation">)</span> <span class="token comment">// 若"name==null" 忽略此属性</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JsonInclude</span><span class="token punctuation">(</span>value<span class="token operator">=</span> <span class="token class-name">JsonInclude<span class="token punctuation">.</span>Include</span><span class="token punctuation">.</span>NON_EMPTY<span class="token punctuation">)</span>  <span class="token comment">// 若hobby长度为0或==null 忽略此属性</span>
    <span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> hobby<span class="token punctuation">;</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    get<span class="token operator">/</span>set
<span class="token punctuation">&#125;</span>
如果name<span class="token operator">=</span><span class="token keyword">null</span><span class="token punctuation">,</span>且 hobby长度为<span class="token number">0</span>，则输出json时：<span class="token punctuation">&#123;</span><span class="token string">"id"</span><span class="token operator">:</span>xx<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="7-5-5-自定义序列化"><a href="#7-5-5-自定义序列化" class="headerlink" title="7.5.5 自定义序列化"></a>7.5.5 自定义序列化</h5><blockquote>
<p>@JsonSerialize(using &#x3D; MySerializer.class) &#x2F;&#x2F; 使用MySerializer输出某属性</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JsonSerialize</span><span class="token punctuation">(</span>using <span class="token operator">=</span> <span class="token class-name">MySerializer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">Double</span> salary <span class="token operator">=</span> <span class="token number">10000.126</span><span class="token punctuation">;</span><span class="token comment">//在输出此属性时，使用MySerializer输出</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    get<span class="token operator">/</span>set
<span class="token punctuation">&#125;</span>
则输出json时：<span class="token punctuation">&#123;</span><span class="token string">"id"</span><span class="token operator">:</span>xx<span class="token punctuation">,</span><span class="token string">"name"</span><span class="token operator">:</span><span class="token string">"xxx"</span><span class="token punctuation">,</span><span class="token string">"salary"</span><span class="token operator">:</span><span class="token number">10000.13</span><span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MySerializer</span> <span class="token keyword">extends</span> <span class="token class-name">JsonSerializer</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Double</span><span class="token punctuation">></span></span> <span class="token punctuation">&#123;</span>

    <span class="token comment">// value即 Double salary的值</span>
    <span class="token annotation punctuation">@Override</span> 
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">serialize</span><span class="token punctuation">(</span><span class="token class-name">Double</span> value<span class="token punctuation">,</span> <span class="token class-name">JsonGenerator</span> gen<span class="token punctuation">,</span> <span class="token class-name">SerializerProvider</span> serializers<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 将Double salary的值 四舍五入</span>
        <span class="token class-name">String</span> number <span class="token operator">=</span> <span class="token class-name">BigDecimal</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setScale</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token class-name">BigDecimal</span><span class="token punctuation">.</span>ROUND_HALF_UP<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 输出 四舍五入后的值</span>
        gen<span class="token punctuation">.</span><span class="token function">writeNumber</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="7-6-FastJson"><a href="#7-6-FastJson" class="headerlink" title="7.6 FastJson"></a>7.6 FastJson</h4><h5 id="7-6-1-导入依赖"><a href="#7-6-1-导入依赖" class="headerlink" title="7.6.1 导入依赖"></a>7.6.1 导入依赖</h5><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- FastJson --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>fastjson<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.2.54<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="7-6-2-安装FastJson"><a href="#7-6-2-安装FastJson" class="headerlink" title="7.6.2 安装FastJson"></a>7.6.2 安装FastJson</h5><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>annotation-driven</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- 安装FastJson,转换器 --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>message-converters</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            <span class="token comment">&lt;!-- 声明转换类型:json --></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>supportedMediaTypes<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>list</span><span class="token punctuation">></span></span>
                    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>value</span><span class="token punctuation">></span></span>application/json<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>value</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>list</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>property</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>bean</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">mvc:</span>message-converters</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">mvc:</span>annotation-driven</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="7-6-3-使用"><a href="#7-6-3-使用" class="headerlink" title="7.6.3 使用"></a>7.6.3 使用</h5><blockquote>
<p>@ResponseBody  @RequestBody @RestController 使用方法不变</p>
</blockquote>
<h5 id="7-6-4-常用注解"><a href="#7-6-4-常用注解" class="headerlink" title="7.6.4 常用注解"></a>7.6.4 常用注解</h5><blockquote>
<ul>
<li>日期格式化：@JSONField(format&#x3D;”yyyy&#x2F;MM&#x2F;dd”)</li>
<li>属性名修改：@JSONField(name&#x3D;”birth”）</li>
<li>忽略属性：@JSONField(serialize &#x3D; false)</li>
<li>包含null值：@JSONField(serialzeFeatures &#x3D; SerializerFeature.WriteMapNullValue)  默认会忽略所有null值,有此注解会输出null<ul>
<li>@JSONField(serialzeFeatures &#x3D; SerializerFeature.WriteNullStringAsEmpty)  null的String输出为””</li>
</ul>
</li>
<li>自定义序列化：@JSONField(serializeUsing &#x3D; MySerializer2.class)</li>
</ul>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span><span class="token punctuation">&#123;</span>
	<span class="token annotation punctuation">@JSONField</span><span class="token punctuation">(</span>serialize <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JSONField</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"NAME"</span><span class="token punctuation">,</span>serialzeFeatures <span class="token operator">=</span> <span class="token class-name">SerializerFeature<span class="token punctuation">.</span>WriteNullStringAsEmpty</span><span class="token punctuation">)</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JSONField</span><span class="token punctuation">(</span>serialzeFeatures <span class="token operator">=</span> <span class="token class-name">SerializerFeature<span class="token punctuation">.</span>WriteMapNullValue</span><span class="token punctuation">)</span> 
    <span class="token keyword">private</span> <span class="token class-name">String</span> city<span class="token punctuation">;</span>
	<span class="token annotation punctuation">@JSONField</span><span class="token punctuation">(</span>format<span class="token operator">=</span><span class="token string">"yyyy/MM/dd"</span><span class="token punctuation">)</span>
	<span class="token keyword">private</span> <span class="token class-name">Date</span> birth<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JSONField</span><span class="token punctuation">(</span>serializeUsing <span class="token operator">=</span> <span class="token class-name">MySerializer2</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">Double</span> salary<span class="token punctuation">;</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MySerializer2</span> <span class="token keyword">implements</span> <span class="token class-name">ObjectSerializer</span> <span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token class-name">JSONSerializer</span> serializer<span class="token punctuation">,</span> <span class="token class-name">Object</span> object<span class="token punctuation">,</span> <span class="token class-name">Object</span> fieldName<span class="token punctuation">,</span> <span class="token class-name">Type</span> fieldType<span class="token punctuation">,</span>
                      <span class="token keyword">int</span> features<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">Double</span> value <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Double</span><span class="token punctuation">)</span> object<span class="token punctuation">;</span> <span class="token comment">// salary属性值</span>
        <span class="token class-name">String</span> text <span class="token operator">=</span> value <span class="token operator">+</span> <span class="token string">"元"</span><span class="token punctuation">;</span><span class="token comment">// 在salary后拼接 “元”</span>
        serializer<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 输出拼接后的内容</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token number">1</span>，<span class="token keyword">null</span>，<span class="token keyword">null</span>，<span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span>，<span class="token number">100.5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 如上对象，转换json：</span>
<span class="token punctuation">&#123;</span>NAME<span class="token operator">:</span><span class="token string">""</span>，city<span class="token operator">:</span><span class="token keyword">null</span>，<span class="token string">"birth"</span><span class="token operator">:</span><span class="token string">"2020/12/12"</span>，<span class="token string">"salary"</span><span class="token operator">:</span><span class="token string">"100.5元"</span><span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h3 id="八、异常解析器"><a href="#八、异常解析器" class="headerlink" title="八、异常解析器"></a>八、异常解析器</h3><hr>
<p>两种方式：</p>
<ul>
<li>实现 HandlerExceptionResolver</li>
<li>通过 @ExceptionHandler 注解定义</li>
<li>SimpleMappingExceptionResolver</li>
</ul>
<h4 id="8-1-现有方案，分散处理"><a href="#8-1-现有方案，分散处理" class="headerlink" title="8.1 现有方案，分散处理"></a>8.1 现有方案，分散处理</h4><blockquote>
<p>Controller中的每个Handler自己处理异常</p>
<p>此种处理方案，异常处理逻辑，分散在各个handler中，不利于集中管理</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">xxx</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">try</span><span class="token punctuation">&#123;</span>
    	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token punctuation">&#125;</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Exception1</span> e<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    	e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"redirect:/xx/error1"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Exception2</span> e<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    	e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"redirect:/xx/error2"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="8-2-异常解析器，统一处理"><a href="#8-2-异常解析器，统一处理" class="headerlink" title="8.2 异常解析器，统一处理"></a>8.2 异常解析器，统一处理</h4><blockquote>
<p>Controller中的每个Handler不再自己处理异常，而是直接throws所有异常。</p>
<p>定义一个“异常解析器” 集中捕获处理 所有异常</p>
<p>此种方案，在集中管理异常方面，更有优势！</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyExResolver</span> <span class="token keyword">implements</span> <span class="token class-name">HandlerExceptionResolver</span><span class="token punctuation">&#123;</span>
	<span class="token comment">/**
	 * 异常解析器：主体逻辑
	 * 执行时刻：当handler中抛出异常时，会执行：捕获异常，并可以跳到错误页面
	 */</span>
	<span class="token annotation punctuation">@Override</span>
	<span class="token keyword">public</span> <span class="token class-name">ModelAndView</span> <span class="token function">resolveException</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span>
			<span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">,</span> <span class="token class-name">Exception</span> ex<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		ex<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//打印异常栈</span>
		<span class="token comment">//创建一个ModelAndView</span>
		<span class="token class-name">ModelAndView</span> mv <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ModelAndView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//识别异常</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span>ex <span class="token keyword">instanceof</span> <span class="token class-name">Exception1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			mv<span class="token punctuation">.</span><span class="token function">setViewName</span><span class="token punctuation">(</span><span class="token string">"redirect:/xxx/error1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ex <span class="token keyword">instanceof</span> <span class="token class-name">Exception2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			mv<span class="token punctuation">.</span><span class="token function">setViewName</span><span class="token punctuation">(</span><span class="token string">"redirect:/xxx/error2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
			mv<span class="token punctuation">.</span><span class="token function">setViewName</span><span class="token punctuation">(</span><span class="token string">"redirect:/xxx/error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> mv<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- 声明异常解析器 --></span>	
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.baizhi.exception.resolver.MyExResolver<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h3 id="九、拦截器"><a href="#九、拦截器" class="headerlink" title="九、拦截器"></a>九、拦截器</h3><p>和过滤器的区别：</p>
<ul>
<li><strong>执行时机：拦截器晚于过滤器。</strong></li>
<li>拦截器是一种 AOP 风格的过滤器。</li>
</ul>
<hr>
<h4 id="9-1-作用"><a href="#9-1-作用" class="headerlink" title="9.1 作用"></a>9.1 作用</h4><blockquote>
<p>作用：抽取handler中的冗余功能</p>
</blockquote>
<h4 id="9-2-定义拦截器"><a href="#9-2-定义拦截器" class="headerlink" title="9.2 定义拦截器"></a>9.2 定义拦截器</h4><blockquote>
<p>执行顺序： preHandle–postHandle–afterCompletion</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyInter1</span> <span class="token keyword">implements</span> <span class="token class-name">HandlerInterceptor</span><span class="token punctuation">&#123;</span>
	<span class="token comment">//主要逻辑：在handler之前执行：抽取handler中的冗余代码</span>
	<span class="token annotation punctuation">@Override</span>
	<span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">preHandle</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span>
			<span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"pre~~~"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/*
        response.sendRedirect("/springMVC_day2/index.jsp");//响应
        return false;//中断请求
        */</span>
		<span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token comment">//放行，后续的拦截器或handler就会执行</span>
	<span class="token punctuation">&#125;</span>
	<span class="token comment">//在handler之后执行:进一步的响应定制</span>
	<span class="token annotation punctuation">@Override</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">postHandle</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span>
			<span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">,</span>
			<span class="token class-name">ModelAndView</span> modelAndView<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"post~~"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token comment">//在页面渲染完毕之后，执行：资源回收</span>
	<span class="token annotation punctuation">@Override</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterCompletion</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span>
			<span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">,</span> <span class="token class-name">Exception</span> ex<span class="token punctuation">)</span>
			<span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"after~~"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="9-3-配置拦截路径"><a href="#9-3-配置拦截路径" class="headerlink" title="9.3 配置拦截路径"></a>9.3 配置拦截路径</h4><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>interceptors</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>interceptor</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>mapping</span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/inter/test1<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>mapping</span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/inter/test2<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>mapping</span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/inter/test*<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment">&lt;!-- test开头 --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>mapping</span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/inter/**<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment">&lt;!-- /** 任意多级任意路径 --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">mvc:</span>exclude-mapping</span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/inter/a/**<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>   <span class="token comment">&lt;!--不拦截此路径--></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.baizhi.interceptor.MyInter1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>bean</span><span class="token punctuation">></span></span>   <span class="token comment">&lt;!--拦截器类--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">mvc:</span>interceptor</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">mvc:</span>interceptors</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="十、上传"><a href="#十、上传" class="headerlink" title="十、上传"></a>十、上传</h3><hr>
<h4 id="10-1-导入jar"><a href="#10-1-导入jar" class="headerlink" title="10.1 导入jar"></a>10.1 导入jar</h4><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>commons-io<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>commons-io<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>commons-fileupload<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>commons-fileupload<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.3.3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusions</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusion</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>javax.servlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>servlet-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusion</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusions</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="10-2-表单"><a href="#10-2-表单" class="headerlink" title="10.2 表单"></a>10.2 表单</h4><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">action</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>$&#123;pageContext.request.contextPath &#125;/upload/test1<span class="token punctuation">"</span></span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post<span class="token punctuation">"</span></span> 
      <span class="token attr-name">enctype</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>multipart/form-data<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  file: <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>file<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>source<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>submit<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>提交<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="10-3-上传解析器"><a href="#10-3-上传解析器" class="headerlink" title="10.3 上传解析器"></a>10.3 上传解析器</h4><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- 上传解析器 
	     id必须是：“multipartResolver”
 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>multipartResolver<span class="token punctuation">"</span></span> 
      <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.springframework.web.multipart.commons.CommonsMultipartResolver<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- 最大可上传的文件大小  单位：byte  超出后会抛出MaxUploadSizeExceededException异常，可以异常解析器捕获 --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>maxUploadSize<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>1048576<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>property</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="10-4-Handler"><a href="#10-4-Handler" class="headerlink" title="10.4 Handler"></a>10.4 Handler</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello1</span><span class="token punctuation">(</span><span class="token class-name">String</span> username<span class="token punctuation">,</span><span class="token class-name">MultipartFile</span> source<span class="token punctuation">,</span><span class="token class-name">HttpSession</span> session<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">//文件的原始名称</span>
    <span class="token class-name">String</span> filename <span class="token operator">=</span> source<span class="token punctuation">.</span><span class="token function">getOriginalFilename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//定制全局唯一的命名</span>
    <span class="token class-name">String</span> unique <span class="token operator">=</span> UUID<span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//获得文件的后缀</span>
    <span class="token class-name">String</span> ext <span class="token operator">=</span> <span class="token class-name">FilenameUtils</span><span class="token punctuation">.</span><span class="token function">getExtension</span><span class="token punctuation">(</span>filename<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//abc.txt   txt    hello.html  html</span>
    <span class="token comment">//定制全局唯一的文件名</span>
    <span class="token class-name">String</span> uniqueFileName <span class="token operator">=</span> unique<span class="token operator">+</span><span class="token string">"."</span><span class="token operator">+</span>ext<span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"唯一的文件名:"</span><span class="token operator">+</span>uniqueFileName<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//文件的类型</span>
    <span class="token class-name">String</span> type <span class="token operator">=</span> source<span class="token punctuation">.</span><span class="token function">getContentType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"filename:"</span><span class="token operator">+</span>filename<span class="token operator">+</span><span class="token string">" type:"</span><span class="token operator">+</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//获得 upload_file的磁盘路径 ==> 在webapp目录下创建一个目录"upload_file",且此目录初始不要为空，否则编译时被忽略</span>
    <span class="token class-name">String</span> real_path <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token string">"/upload_file"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"real_path:"</span><span class="token operator">+</span>real_path<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//将上传的文件，存入磁盘路径中</span>
    <span class="token comment">//source.transferTo(new File("d:/xxxx/xxxx/xx.jpg"))</span>
    source<span class="token punctuation">.</span><span class="token function">transferTo</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>real_path<span class="token operator">+</span><span class="token string">"\\"</span><span class="token operator">+</span>uniqueFileName<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"index"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="十一、下载"><a href="#十一、下载" class="headerlink" title="十一、下载"></a>十一、下载</h3><hr>
<h4 id="11-1-超链"><a href="#11-1-超链" class="headerlink" title="11.1 超链"></a>11.1 超链</h4><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>$&#123;pageContext.request.contextPath&#125;/download/test1?name=Koala.jpg<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>下载<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h4 id="11-2-Handler"><a href="#11-2-Handler" class="headerlink" title="11.2 Handler"></a>11.2 Handler</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test1"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">hello1</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span><span class="token class-name">HttpSession</span> session<span class="token punctuation">,</span><span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"name:"</span><span class="token operator">+</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//获得要下载文件的绝对路径</span>
    <span class="token class-name">String</span> path <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token string">"/upload_file"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//文件的完整路径</span>
    <span class="token class-name">String</span> real_path <span class="token operator">=</span> path<span class="token operator">+</span><span class="token string">"\\"</span><span class="token operator">+</span>name<span class="token punctuation">;</span>

    <span class="token comment">//设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名</span>
    response<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"content-disposition"</span><span class="token punctuation">,</span><span class="token string">"attachment;filename="</span><span class="token operator">+</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//读取目标文件，写出给客户端</span>
    <span class="token class-name">IOUtils</span><span class="token punctuation">.</span><span class="token function">copy</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>real_path<span class="token punctuation">)</span><span class="token punctuation">,</span> response<span class="token punctuation">.</span><span class="token function">getOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//上一步，已经是响应了,所以此handler直接是void</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="十二、验证码"><a href="#十二、验证码" class="headerlink" title="十二、验证码"></a>十二、验证码</h3><hr>
<h4 id="12-1-作用"><a href="#12-1-作用" class="headerlink" title="12.1 作用"></a>12.1 作用</h4><blockquote>
<p>防止暴力攻击，前端安全保障</p>
</blockquote>
<h4 id="12-2-导入jar"><a href="#12-2-导入jar" class="headerlink" title="12.2 导入jar"></a>12.2 导入jar</h4><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- Kaptcha --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.github.penggle<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>kaptcha<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.3.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusions</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusion</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>javax.servlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>javax.servlet-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusion</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusions</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="12-3-声明验证码组件"><a href="#12-3-声明验证码组件" class="headerlink" title="12.3 声明验证码组件"></a>12.3 声明验证码组件</h4><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">></span></span>cap<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-class</span><span class="token punctuation">></span></span>com.google.code.kaptcha.servlet.KaptchaServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-class</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>init-param</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-name</span><span class="token punctuation">></span></span>kaptcha.border<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-name</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-value</span><span class="token punctuation">></span></span>no<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-value</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>init-param</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>init-param</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-name</span><span class="token punctuation">></span></span>kaptcha.textproducer.char.length<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-name</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-value</span><span class="token punctuation">></span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-value</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>init-param</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>init-param</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-name</span><span class="token punctuation">></span></span>kaptcha.textproducer.char.string<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-name</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-value</span><span class="token punctuation">></span></span>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-value</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>init-param</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>init-param</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-name</span><span class="token punctuation">></span></span>kaptcha.background.clear.to<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-name</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-value</span><span class="token punctuation">></span></span>211,229,237<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-value</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>init-param</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>init-param</span><span class="token punctuation">></span></span>
      <span class="token comment">&lt;!-- session.setAttribute("captcha","验证码") --></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-name</span><span class="token punctuation">></span></span>kaptcha.session.key<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-name</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>param-value</span><span class="token punctuation">></span></span>captcha<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>param-value</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>init-param</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-mapping</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">></span></span>cap<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">></span></span>/captcha<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-mapping</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="12-4-Page"><a href="#12-4-Page" class="headerlink" title="12.4 Page"></a>12.4 Page</h4><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>$&#123;pageContext.request.contextPath&#125;/captcha<span class="token punctuation">"</span></span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token value css language-css"><span class="token property">width</span><span class="token punctuation">:</span>85px</span><span class="token punctuation">"</span></span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>cap<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
    <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#cap"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">click</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//刷新验证码</span>
            path <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">attr</span><span class="token punctuation">(</span><span class="token string">"src"</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"?"</span><span class="token operator">+</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">attr</span><span class="token punctuation">(</span><span class="token string">"src"</span><span class="token punctuation">,</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="十三、REST"><a href="#十三、REST" class="headerlink" title="十三、REST"></a>十三、REST</h3><hr>
<h4 id="13-1-开发风格"><a href="#13-1-开发风格" class="headerlink" title="13.1 开发风格"></a>13.1 开发风格</h4><blockquote>
<p>是一种开发风格，遵从此风格开发软件，符合REST风格，则RESTful。</p>
</blockquote>
<blockquote>
<p>两个核心要求：</p>
<ul>
<li>每个资源都有唯一的标识(URL)</li>
<li>不同的行为，使用对应的http-method</li>
</ul>
</blockquote>
<table>
<thead>
<tr>
<th>访问标识</th>
<th>资源</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users">http://localhost:8989/xxx/users</a></td>
<td>所有用户</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users/1">http://localhost:8989/xxx/users/1</a></td>
<td>用户1</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users/1/orders">http://localhost:8989/xxx/users/1/orders</a></td>
<td>用户1的所有订单</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>请求方式</th>
<th>标识</th>
<th>意图</th>
</tr>
</thead>
<tbody><tr>
<td>GET（查询）</td>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users">http://localhost:8989/xxx/users</a></td>
<td>查询所有用户</td>
</tr>
<tr>
<td>POST（添加）</td>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users">http://localhost:8989/xxx/users</a></td>
<td>在所有用户中增加一个</td>
</tr>
<tr>
<td>PUT（更新）</td>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users">http://localhost:8989/xxx/users</a></td>
<td>在所有用户中修改一个</td>
</tr>
<tr>
<td>DELETE（删除）</td>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users/1">http://localhost:8989/xxx/users/1</a></td>
<td>删除用户1</td>
</tr>
<tr>
<td>GET</td>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users/1">http://localhost:8989/xxx/users/1</a></td>
<td>查询用户1</td>
</tr>
<tr>
<td>GET</td>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users/1/orders">http://localhost:8989/xxx/users/1/orders</a></td>
<td>查询用户1的所有订单</td>
</tr>
<tr>
<td>POST</td>
<td><a target="_blank" rel="noopener" href="http://localhost:8989/xxx/users/1/orders">http://localhost:8989/xxx/users/1/orders</a></td>
<td>在用户1的所有订单中增加一个</td>
</tr>
</tbody></table>
<h4 id="13-2-优点"><a href="#13-2-优点" class="headerlink" title="13.2 优点"></a>13.2 优点</h4><blockquote>
<ul>
<li>**输出json：</li>
</ul>
</blockquote>
<h4 id="13-3-使用"><a href="#13-3-使用" class="headerlink" title="13.3 使用"></a>13.3 使用</h4><h5 id="13-3-1-定义Rest风格的-Controller"><a href="#13-3-1-定义Rest风格的-Controller" class="headerlink" title="13.3.1 定义Rest风格的 Controller"></a>13.3.1 定义Rest风格的 Controller</h5><blockquote>
<p>@RequestMapping(value&#x3D;”&#x2F;users”,method &#x3D; RequestMethod.GET)</p>
<p>等价</p>
<p>@GetMapping(“&#x2F;users”)</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RestController</span> <span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/users"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">></span></span> <span class="token function">queryAllUsers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"get"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">></span></span> users <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
        <span class="token keyword">return</span> users<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@PostMapping</span><span class="token punctuation">(</span><span class="token string">"/users"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">addUser</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">User</span> user<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Post user :"</span><span class="token operator">+</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"&#123;status:1&#125;"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    
    <span class="token annotation punctuation">@PutMapping</span><span class="token punctuation">(</span><span class="token string">"/users"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">updateUser</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">User</span> user<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Put user"</span> user<span class="token operator">:</span>"<span class="token operator">+</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"&#123;status:1&#125;"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/users/&#123;id&#125;"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">queryOneUser</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span> <span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//@PathVariable 接收路径中的值</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Get user id:"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"&#123;status:1&#125;"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@DeleteMapping</span><span class="token punctuation">(</span><span class="token string">"/users/&#123;id&#125;"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">deleteOneUser</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span> <span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//@PathVariable 接收路径中的值</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"delete user id:"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"&#123;status:1&#125;"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="13-3-2-Ajax请求"><a href="#13-3-2-Ajax请求" class="headerlink" title="13.3.2 Ajax请求"></a>13.3.2 Ajax请求</h5><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">    
	<span class="token keyword">function</span> <span class="token function">putUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">// 发送更新请求 （增加请求发送方式也是如此）</span>
        <span class="token keyword">var</span> xhr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XMLHttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//定义 put，delete,get,post方式 即可，不用定义_method</span>
        xhr<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"put"</span><span class="token punctuation">,</span><span class="token string">"$&#123;pageContext.request.contextPath&#125;/rest04/users"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">// 设置请求头</span>
        xhr<span class="token punctuation">.</span><span class="token function">setRequestHeader</span><span class="token punctuation">(</span><span class="token string">"content-type"</span><span class="token punctuation">,</span><span class="token string">"application/json"</span><span class="token punctuation">)</span>；
        <span class="token comment">// 设置请求参数</span>
        <span class="token keyword">var</span> user <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token literal-property property">id</span><span class="token operator">:</span><span class="token number">1</span>，<span class="token constant">NAME</span><span class="token operator">:</span><span class="token string">"shine"</span><span class="token literal-property property">，city</span><span class="token operator">:</span><span class="token string">"bj"</span>，<span class="token string">"birth"</span><span class="token operator">:</span><span class="token string">"2020/12/12"</span>，<span class="token string">"salary"</span><span class="token operator">:</span><span class="token number">100.5</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
        xhr<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        xhr<span class="token punctuation">.</span><span class="token function-variable function">onreadystatechange</span><span class="token operator">=</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>xhr<span class="token punctuation">.</span>readyState<span class="token operator">==</span><span class="token number">4</span> <span class="token operator">&amp;&amp;</span> xhr<span class="token punctuation">.</span>status<span class="token operator">==</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token keyword">var</span> ret <span class="token operator">=</span> xhr<span class="token punctuation">.</span>responseText<span class="token punctuation">;</span>
                <span class="token comment">// 解析json，并输出</span>
                console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    	<span class="token comment">/*$.ajax(&#123;
            url:'$&#123;pageContext.request.contextPath&#125;/rest04/users',
            type:'put',
            contentType:"application/json",//声明请求参数类型为 json
            data:JSON.stringify(user),// 转换js对象成json
            success:function(ret)&#123;
                console.log(JSON.parse(ret));
            &#125;
        &#125;);*/</span>
    <span class="token punctuation">&#125;</span>

	<span class="token keyword">function</span> <span class="token function">delUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token comment">// 发送删除请求</span>
        <span class="token keyword">var</span> xhr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XMLHttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//定义 put，delete,get,post方式 即可，不用定义_method</span>
        xhr<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"delete"</span><span class="token punctuation">,</span><span class="token string">"$&#123;pageContext.request.contextPath&#125;/rest04/users/1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        xhr<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        xhr<span class="token punctuation">.</span><span class="token function-variable function">onreadystatechange</span><span class="token operator">=</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>xhr<span class="token punctuation">.</span>readyState<span class="token operator">==</span><span class="token number">4</span> <span class="token operator">&amp;&amp;</span> xhr<span class="token punctuation">.</span>status<span class="token operator">==</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token keyword">var</span> ret <span class="token operator">=</span> xhr<span class="token punctuation">.</span>responseText<span class="token punctuation">;</span>
                console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="十四、跨域请求"><a href="#十四、跨域请求" class="headerlink" title="十四、跨域请求"></a>十四、跨域请求</h3><hr>
<h4 id="14-1-域"><a href="#14-1-域" class="headerlink" title="14.1 域"></a>14.1 域</h4><blockquote>
<p>域：协议+IP（域名）+端口</p>
<ul>
<li><p><a target="_blank" rel="noopener" href="http://localhost:8989/">http://localhost:8989</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://localhost:8080/">http://localhost:8080</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://www.baidu.com/">http://www.baidu.com:80</a></p>
</li>
</ul>
</blockquote>
<h4 id="14-2-Ajax跨域问题"><a href="#14-2-Ajax跨域问题" class="headerlink" title="14.2 Ajax跨域问题"></a>14.2 Ajax跨域问题</h4><blockquote>
<ul>
<li><p>Ajax发送请求时，不允许跨域，以防用户信息泄露。</p>
</li>
<li><p>当Ajax跨域请求时，响应会被浏览器拦截(同源策略)，并报错。即浏览器默认不允许ajax跨域得到响应内容。</p>
</li>
<li><p>互相信任的域之间如果需要ajax访问，(比如前后端分离项目中，前端项目和后端项目之间)，则需要额外的设置才可正常请求。</p>
</li>
</ul>
</blockquote>
<h4 id="14-3-解决方案"><a href="#14-3-解决方案" class="headerlink" title="14.3 解决方案"></a>14.3 解决方案</h4><blockquote>
<ul>
<li><p>允许其他域访问</p>
</li>
<li><p>在被访问方的Controller类上，添加注解</p>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@CrossOrigin</span><span class="token punctuation">(</span><span class="token string">"http://localhost:8080"</span><span class="token punctuation">)</span> <span class="token comment">//允许此域发请求访问</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SysUserController</span> <span class="token punctuation">&#123;</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<blockquote>
<ul>
<li><p>携带对方cookie，使得session可用</p>
</li>
<li><p>在访问方，ajax中添加属性：withCredentials: true</p>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">$<span class="token punctuation">.</span><span class="token function">ajax</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
     <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"POST"</span><span class="token punctuation">,</span>
     <span class="token literal-property property">url</span><span class="token operator">:</span> <span class="token string">"http://localhost:8989/web/sys/login"</span><span class="token punctuation">,</span>
     <span class="token operator">...</span><span class="token punctuation">,</span>
     <span class="token literal-property property">xhrFields</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
       <span class="token comment">// 跨域携带cookie</span>
       <span class="token literal-property property">withCredentials</span><span class="token operator">:</span> <span class="token boolean">true</span>
     <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
或
<span class="token keyword">var</span> xhr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XMLHttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 跨域携带cookie</span>
xhr<span class="token punctuation">.</span>withCredentials<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="十五、SpringMVC执行流程"><a href="#十五、SpringMVC执行流程" class="headerlink" title="十五、SpringMVC执行流程"></a>十五、SpringMVC执行流程</h3><hr>
<table>
<thead>
<tr>
<th align="center"></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><img src="/Pictures/springMVC%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B.jpg" alt="springMVC执行流程"></td>
</tr>
</tbody></table>
<h3 id="十六、Spring整合"><a href="#十六、Spring整合" class="headerlink" title="十六、Spring整合"></a>十六、Spring整合</h3><hr>
<h4 id="16-1-整合思路"><a href="#16-1-整合思路" class="headerlink" title="16.1 整合思路"></a>16.1 整合思路</h4><blockquote>
<p>此时项目中有两个工厂</p>
<ul>
<li>DispatcherServlet 启动的springMVC工厂&#x3D;&#x3D;负责生产C及springMVC自己的系统组件</li>
<li>ContextLoaderListener 启动的spring工厂&#x3D;&#x3D;负责生产其他所有组件</li>
<li>springMVC的工厂会被设置为spring工厂的子工厂，可以随意获取spring工厂中的组件</li>
<li>整合过程，就是累加：代码+依赖+配置。然后将service注入给controller即可</li>
</ul>
</blockquote>
<h4 id="16-2-整合技巧"><a href="#16-2-整合技巧" class="headerlink" title="16.2 整合技巧"></a>16.2 整合技巧</h4><p>Spring 是一个父容器，SpringMVC 是一个子容器，子容器中可以访问父容器的组件，父容器不可以访问子容器的组件。</p>
<p>例如，我们将 Service、Dao、Component 都注册到 Spring 容器中，将 Controller 注册到 SpringMVC 容器中，则在 Controller 中，可以注入 Service、Dao、Component ；但是 Service、Dao、Component 中无法注入 Controller。</p>
<blockquote>
<p>两个工厂不能有彼此侵入，即，生产的组件不能有重合。</p>
</blockquote>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- 告知SpringMVC  哪些包中 存在 被注解的类
	use-default-filters=true 凡是被 @Controller @Service  @Repository注解的类，都会被扫描
	use-default-filters=false 默认不扫描包内的任何类, 只扫描include-filter中指定的类
	只扫描被@Controller注解的类
--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">context:</span>component-scan</span> <span class="token attr-name">base-package</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.zhj<span class="token punctuation">"</span></span> <span class="token attr-name">use-default-filters</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
 	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">context:</span>include-filter</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>annotation<span class="token punctuation">"</span></span> <span class="token attr-name">expression</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.springframework.stereotype.Controller<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">context:</span>component-scan</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- 告知Spring
     唯独不扫描@Controller注解的类 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">context:</span>component-scan</span> <span class="token attr-name">base-package</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.zhj<span class="token punctuation">"</span></span> <span class="token attr-name">use-default-filters</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">context:</span>exclude-filter</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>annotation<span class="token punctuation">"</span></span> <span class="token attr-name">expression</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.springframework.stereotype.Controller<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">context:</span>component-scan</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>服务端数据校验：<a target="_blank" rel="noopener" href="http://springmvc.javaboy.org/2019/1112/validation">http://springmvc.javaboy.org/2019/1112/validation</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">hengxingstu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://hengxingstu.gitee.io/2023/02/21/code/springMVC%E6%96%87%E6%A1%A3/">https://hengxingstu.gitee.io/2023/02/21/code/springMVC%E6%96%87%E6%A1%A3/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://hengxingstu.gitee.io" target="_blank">恒星同学</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Spring/">Spring</a></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2023/02/21/vdVhPU5RITfrXg4.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/2023/02/16/code/SpringMVC%E7%AC%94%E8%AE%B0/"><img class="next-cover" src="https://s2.loli.net/2023/02/16/N8E1LGTUnYVdmPk.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">SpringMVC学习笔记</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/02/15/code/SpringAop%E7%AC%94%E8%AE%B0/" title="SpringAOP笔记"><img class="cover" src="https://s2.loli.net/2023/02/08/jWNelFA4c7BGkTw.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-15</div><div class="title">SpringAOP笔记</div></div></a></div><div><a href="/2023/02/16/code/Spring%E4%BA%8B%E5%8A%A1/" title="Spring学习笔记"><img class="cover" src="https://s2.loli.net/2023/02/16/N8E1LGTUnYVdmPk.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-16</div><div class="title">Spring学习笔记</div></div></a></div><div><a href="/2023/02/12/code/Spring%E7%AC%94%E8%AE%B0/" title="Spring学习笔记"><img class="cover" src="https://s2.loli.net/2023/02/12/ukqiYOMfcU4WwCJ.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-12</div><div class="title">Spring学习笔记</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://wallpapercave.com/uwp/uwp2365506.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">hengxingstu</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">36</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/hengxingstu"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/hengxingstu" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:hengxingstu@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是我的博客，欢迎和我联系，交流是最好的老师</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E3%80%81SpringMVC"><span class="toc-number">1.</span> <span class="toc-text">一、SpringMVC</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-%E5%BC%95%E8%A8%80"><span class="toc-number">1.1.</span> <span class="toc-text">1.1 引言</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-MVC%E6%9E%B6%E6%9E%84"><span class="toc-number">1.2.</span> <span class="toc-text">1.2 MVC架构</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-2-1-%E6%A6%82%E5%BF%B5"><span class="toc-number">1.2.1.</span> <span class="toc-text">1.2.1 概念</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#1-2-2-%E5%A5%BD%E5%A4%84"><span class="toc-number">1.2.2.</span> <span class="toc-text">1.2.2 好处</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%BC%80%E5%8F%91%E6%B5%81%E7%A8%8B"><span class="toc-number">2.</span> <span class="toc-text">二、开发流程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">2.1.</span> <span class="toc-text">2.1 导入依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-%E9%85%8D%E7%BD%AE%E6%A0%B8%E5%BF%83-%E5%89%8D%E7%AB%AF-%E6%8E%A7%E5%88%B6%E5%99%A8"><span class="toc-number">2.2.</span> <span class="toc-text">2.2 配置核心(前端)控制器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-%E5%90%8E%E7%AB%AF%E6%8E%A7%E5%88%B6%E5%99%A8"><span class="toc-number">2.3.</span> <span class="toc-text">2.3 后端控制器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-4-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">2.4.</span> <span class="toc-text">2.4 配置文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-5-%E8%AE%BF%E9%97%AE"><span class="toc-number">2.5.</span> <span class="toc-text">2.5 访问</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E6%8E%A5%E6%94%B6%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0"><span class="toc-number">3.</span> <span class="toc-text">三、接收请求参数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E5%8F%82%E6%95%B0"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 基本类型参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-%E5%AE%9E%E4%BD%93%E6%94%B6%E5%8F%82%E3%80%90%E9%87%8D%E7%82%B9%E3%80%91"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 实体收参【重点】</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-%E6%95%B0%E7%BB%84%E6%94%B6%E5%8F%82"><span class="toc-number">3.3.</span> <span class="toc-text">3.3 数组收参</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-%E9%9B%86%E5%90%88%E6%94%B6%E5%8F%82-%E3%80%90%E4%BA%86%E8%A7%A3%E3%80%91"><span class="toc-number">3.4.</span> <span class="toc-text">3.4 集合收参 【了解】</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-%E8%B7%AF%E5%BE%84%E5%8F%82%E6%95%B0"><span class="toc-number">3.5.</span> <span class="toc-text">3.5 路径参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81"><span class="toc-number">3.6.</span> <span class="toc-text">3.6 中文乱码</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E8%B7%B3%E8%BD%AC"><span class="toc-number">4.</span> <span class="toc-text">四、跳转</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-%E8%BD%AC%E5%8F%91"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 转发</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-%E9%87%8D%E5%AE%9A%E5%90%91"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 重定向</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-%E8%B7%B3%E8%BD%AC%E7%BB%86%E8%8A%82"><span class="toc-number">4.3.</span> <span class="toc-text">4.3 跳转细节</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E4%BC%A0%E5%80%BC"><span class="toc-number">5.</span> <span class="toc-text">五、传值</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-Request%E5%92%8CSession"><span class="toc-number">5.1.</span> <span class="toc-text">5.1 Request和Session</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-JSP%E4%B8%AD%E5%8F%96%E5%80%BC"><span class="toc-number">5.2.</span> <span class="toc-text">5.2 JSP中取值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-3-Model"><span class="toc-number">5.3.</span> <span class="toc-text">5.3 Model</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-4-ModelAndView"><span class="toc-number">5.4.</span> <span class="toc-text">5.4 ModelAndView</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-5-SessionAttributes"><span class="toc-number">5.5.</span> <span class="toc-text">5.5 @SessionAttributes</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90"><span class="toc-number">6.</span> <span class="toc-text">六、静态资源</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1-%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E9%97%AE%E9%A2%98"><span class="toc-number">6.1.</span> <span class="toc-text">6.1 静态资源问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%881"><span class="toc-number">6.2.</span> <span class="toc-text">6.2 解决方案1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-3-%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%882"><span class="toc-number">6.3.</span> <span class="toc-text">6.3 解决方案2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-4-%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%883"><span class="toc-number">6.4.</span> <span class="toc-text">6.4 解决方案3</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%83%E3%80%81Json%E5%A4%84%E7%90%86"><span class="toc-number">7.</span> <span class="toc-text">七、Json处理</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-1-%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">7.1.</span> <span class="toc-text">7.1 导入依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-%E4%BD%BF%E7%94%A8-ResponseBody"><span class="toc-number">7.2.</span> <span class="toc-text">7.2 使用@ResponseBody</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-3-%E4%BD%BF%E7%94%A8-RestController"><span class="toc-number">7.3.</span> <span class="toc-text">7.3 使用@RestController</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-4-%E4%BD%BF%E7%94%A8-RequestBody"><span class="toc-number">7.4.</span> <span class="toc-text">7.4 使用@RequestBody</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#RequestBody-%E6%8E%A5%E6%94%B6Json%E5%8F%82%E6%95%B0"><span class="toc-number">7.5.</span> <span class="toc-text">@RequestBody, 接收Json参数</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#7-4-1-%E5%AE%9A%E4%B9%89Handler"><span class="toc-number">7.5.1.</span> <span class="toc-text">7.4.1 定义Handler</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-4-2-Ajax%E5%8F%91%E9%80%81json"><span class="toc-number">7.5.2.</span> <span class="toc-text">7.4.2 Ajax发送json</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-5-Jackson%E5%B8%B8%E7%94%A8%E6%B3%A8%E8%A7%A3"><span class="toc-number">7.6.</span> <span class="toc-text">7.5 Jackson常用注解</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#7-5-1-%E6%97%A5%E6%9C%9F%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">7.6.1.</span> <span class="toc-text">7.5.1 日期格式化</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-5-2-%E5%B1%9E%E6%80%A7%E5%90%8D%E4%BF%AE%E6%94%B9"><span class="toc-number">7.6.2.</span> <span class="toc-text">7.5.2 属性名修改</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-5-3-%E5%B1%9E%E6%80%A7%E5%BF%BD%E7%95%A5"><span class="toc-number">7.6.3.</span> <span class="toc-text">7.5.3 属性忽略</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-5-4-null%E5%92%8Cempty%E5%B1%9E%E6%80%A7%E6%8E%92%E9%99%A4"><span class="toc-number">7.6.4.</span> <span class="toc-text">7.5.4 null和empty属性排除</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-5-5-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BA%8F%E5%88%97%E5%8C%96"><span class="toc-number">7.6.5.</span> <span class="toc-text">7.5.5 自定义序列化</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-6-FastJson"><span class="toc-number">7.7.</span> <span class="toc-text">7.6 FastJson</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#7-6-1-%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">7.7.1.</span> <span class="toc-text">7.6.1 导入依赖</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-6-2-%E5%AE%89%E8%A3%85FastJson"><span class="toc-number">7.7.2.</span> <span class="toc-text">7.6.2 安装FastJson</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-6-3-%E4%BD%BF%E7%94%A8"><span class="toc-number">7.7.3.</span> <span class="toc-text">7.6.3 使用</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-6-4-%E5%B8%B8%E7%94%A8%E6%B3%A8%E8%A7%A3"><span class="toc-number">7.7.4.</span> <span class="toc-text">7.6.4 常用注解</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E5%BC%82%E5%B8%B8%E8%A7%A3%E6%9E%90%E5%99%A8"><span class="toc-number">8.</span> <span class="toc-text">八、异常解析器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#8-1-%E7%8E%B0%E6%9C%89%E6%96%B9%E6%A1%88%EF%BC%8C%E5%88%86%E6%95%A3%E5%A4%84%E7%90%86"><span class="toc-number">8.1.</span> <span class="toc-text">8.1 现有方案，分散处理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-2-%E5%BC%82%E5%B8%B8%E8%A7%A3%E6%9E%90%E5%99%A8%EF%BC%8C%E7%BB%9F%E4%B8%80%E5%A4%84%E7%90%86"><span class="toc-number">8.2.</span> <span class="toc-text">8.2 异常解析器，统一处理</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B9%9D%E3%80%81%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">9.</span> <span class="toc-text">九、拦截器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#9-1-%E4%BD%9C%E7%94%A8"><span class="toc-number">9.1.</span> <span class="toc-text">9.1 作用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-2-%E5%AE%9A%E4%B9%89%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">9.2.</span> <span class="toc-text">9.2 定义拦截器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-3-%E9%85%8D%E7%BD%AE%E6%8B%A6%E6%88%AA%E8%B7%AF%E5%BE%84"><span class="toc-number">9.3.</span> <span class="toc-text">9.3 配置拦截路径</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E3%80%81%E4%B8%8A%E4%BC%A0"><span class="toc-number">10.</span> <span class="toc-text">十、上传</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#10-1-%E5%AF%BC%E5%85%A5jar"><span class="toc-number">10.1.</span> <span class="toc-text">10.1 导入jar</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-2-%E8%A1%A8%E5%8D%95"><span class="toc-number">10.2.</span> <span class="toc-text">10.2 表单</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-3-%E4%B8%8A%E4%BC%A0%E8%A7%A3%E6%9E%90%E5%99%A8"><span class="toc-number">10.3.</span> <span class="toc-text">10.3 上传解析器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-4-Handler"><span class="toc-number">10.4.</span> <span class="toc-text">10.4 Handler</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E4%B8%80%E3%80%81%E4%B8%8B%E8%BD%BD"><span class="toc-number">11.</span> <span class="toc-text">十一、下载</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#11-1-%E8%B6%85%E9%93%BE"><span class="toc-number">11.1.</span> <span class="toc-text">11.1 超链</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-2-Handler"><span class="toc-number">11.2.</span> <span class="toc-text">11.2 Handler</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E4%BA%8C%E3%80%81%E9%AA%8C%E8%AF%81%E7%A0%81"><span class="toc-number">12.</span> <span class="toc-text">十二、验证码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#12-1-%E4%BD%9C%E7%94%A8"><span class="toc-number">12.1.</span> <span class="toc-text">12.1 作用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#12-2-%E5%AF%BC%E5%85%A5jar"><span class="toc-number">12.2.</span> <span class="toc-text">12.2 导入jar</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#12-3-%E5%A3%B0%E6%98%8E%E9%AA%8C%E8%AF%81%E7%A0%81%E7%BB%84%E4%BB%B6"><span class="toc-number">12.3.</span> <span class="toc-text">12.3 声明验证码组件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#12-4-Page"><span class="toc-number">12.4.</span> <span class="toc-text">12.4 Page</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E4%B8%89%E3%80%81REST"><span class="toc-number">13.</span> <span class="toc-text">十三、REST</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#13-1-%E5%BC%80%E5%8F%91%E9%A3%8E%E6%A0%BC"><span class="toc-number">13.1.</span> <span class="toc-text">13.1 开发风格</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#13-2-%E4%BC%98%E7%82%B9"><span class="toc-number">13.2.</span> <span class="toc-text">13.2 优点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#13-3-%E4%BD%BF%E7%94%A8"><span class="toc-number">13.3.</span> <span class="toc-text">13.3 使用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#13-3-1-%E5%AE%9A%E4%B9%89Rest%E9%A3%8E%E6%A0%BC%E7%9A%84-Controller"><span class="toc-number">13.3.1.</span> <span class="toc-text">13.3.1 定义Rest风格的 Controller</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#13-3-2-Ajax%E8%AF%B7%E6%B1%82"><span class="toc-number">13.3.2.</span> <span class="toc-text">13.3.2 Ajax请求</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E5%9B%9B%E3%80%81%E8%B7%A8%E5%9F%9F%E8%AF%B7%E6%B1%82"><span class="toc-number">14.</span> <span class="toc-text">十四、跨域请求</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#14-1-%E5%9F%9F"><span class="toc-number">14.1.</span> <span class="toc-text">14.1 域</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#14-2-Ajax%E8%B7%A8%E5%9F%9F%E9%97%AE%E9%A2%98"><span class="toc-number">14.2.</span> <span class="toc-text">14.2 Ajax跨域问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#14-3-%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="toc-number">14.3.</span> <span class="toc-text">14.3 解决方案</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E4%BA%94%E3%80%81SpringMVC%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B"><span class="toc-number">15.</span> <span class="toc-text">十五、SpringMVC执行流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E5%85%AD%E3%80%81Spring%E6%95%B4%E5%90%88"><span class="toc-number">16.</span> <span class="toc-text">十六、Spring整合</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#16-1-%E6%95%B4%E5%90%88%E6%80%9D%E8%B7%AF"><span class="toc-number">16.1.</span> <span class="toc-text">16.1 整合思路</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#16-2-%E6%95%B4%E5%90%88%E6%8A%80%E5%B7%A7"><span class="toc-number">16.2.</span> <span class="toc-text">16.2 整合技巧</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/02/21/code/springMVC%E6%96%87%E6%A1%A3/" title="Spring参考文档"><img src="https://s2.loli.net/2023/02/21/vdVhPU5RITfrXg4.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Spring参考文档"/></a><div class="content"><a class="title" href="/2023/02/21/code/springMVC%E6%96%87%E6%A1%A3/" title="Spring参考文档">Spring参考文档</a><time datetime="2023-02-21T12:26:00.000Z" title="发表于 2023-02-21 20:26:00">2023-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/16/code/SpringMVC%E7%AC%94%E8%AE%B0/" title="SpringMVC学习笔记"><img src="https://s2.loli.net/2023/02/16/N8E1LGTUnYVdmPk.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="SpringMVC学习笔记"/></a><div class="content"><a class="title" href="/2023/02/16/code/SpringMVC%E7%AC%94%E8%AE%B0/" title="SpringMVC学习笔记">SpringMVC学习笔记</a><time datetime="2023-02-16T09:06:00.000Z" title="发表于 2023-02-16 17:06:00">2023-02-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/16/code/Spring%E4%BA%8B%E5%8A%A1/" title="Spring学习笔记"><img src="https://s2.loli.net/2023/02/16/N8E1LGTUnYVdmPk.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Spring学习笔记"/></a><div class="content"><a class="title" href="/2023/02/16/code/Spring%E4%BA%8B%E5%8A%A1/" title="Spring学习笔记">Spring学习笔记</a><time datetime="2023-02-16T09:06:00.000Z" title="发表于 2023-02-16 17:06:00">2023-02-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/15/code/SpringAop%E7%AC%94%E8%AE%B0/" title="SpringAOP笔记"><img src="https://s2.loli.net/2023/02/08/jWNelFA4c7BGkTw.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="SpringAOP笔记"/></a><div class="content"><a class="title" href="/2023/02/15/code/SpringAop%E7%AC%94%E8%AE%B0/" title="SpringAOP笔记">SpringAOP笔记</a><time datetime="2023-02-15T13:00:00.000Z" title="发表于 2023-02-15 21:00:00">2023-02-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/12/code/Spring%E7%AC%94%E8%AE%B0/" title="Spring学习笔记"><img src="https://s2.loli.net/2023/02/12/ukqiYOMfcU4WwCJ.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Spring学习笔记"/></a><div class="content"><a class="title" href="/2023/02/12/code/Spring%E7%AC%94%E8%AE%B0/" title="Spring学习笔记">Spring学习笔记</a><time datetime="2023-02-12T05:58:00.000Z" title="发表于 2023-02-12 13:58:00">2023-02-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By hengxingstu</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: 'https://twikoo-hengxing.vercel.app/',
      region: '',
      onCommentLoaded: function () {
        btf.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.vemoji)'))
      }
    }, null))
  }

  const getCount = () => {
    twikoo.getCommentsCount({
      envId: 'https://twikoo-hengxing.vercel.app/',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      document.getElementById('twikoo-count').innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const runFn = () => {
    init()
    GLOBAL_CONFIG_SITE.isPost && getCount()
  }

  const loadTwikoo = () => {
    if (typeof twikoo === 'object') {
      setTimeout(runFn,0)
      return
    } 
    getScript('https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js').then(runFn)
  }

  if ('Twikoo' === 'Twikoo' || !false) {
    if (false) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo()
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/live2dw/assets/haru02.model.json"},"display":{"superSample":2,"width":200,"height":400,"position":"right","hOffset":10,"vOffset":0},"mobile":{"show":false,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.2},"dialog":{"enable":true,"hitokoto":true},"log":false});</script></body></html>